<!--
 * @Author: your name
 * @Date: 2021-06-17 18:58:38
 * @LastEditTime: 2021-06-19 20:06:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \agriculture-ui\src\pages\cms\Article.vue
-->
  <template>
  <div>
    <h2>{{title}}</h2>
    <el-form inline :model="param" size="small">
      <el-form-item label="标题">
        <el-input v-model="param.title"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table
       
      :data="articles.list"
      style="width: 100%">
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="status" label="状态" width="180"> </el-table-column>
      <el-table-column prop="readTimes" label="阅读次数" width="200"> </el-table-column>
      <el-table-column label="操作" width="200" align="center"> 
        <!-- 定制 -->
        <template v-slot="scope">
          <el-button @click="delHandler(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="pageChangeHandler"
      :current-page="articles.page"
      :page-size="articles.pageSize"
      :total="articles.total">
    </el-pagination>


  </div>
</template>
<script>
import {get,post} from '@/utils/request'
export default {
  // 1. 数据，变量
  data(){
    return {
      title:"文章管理",
      articles:[],
      param:{
        page:1,
        pageSize:5
      }
    }
  },
  // 2. 方法
  methods:{
    //2.3 翻页
    pageChangeHandler(page){
      this.param.page = page;
      this.loadData();
    },
    //2.2 根据id删除
    delHandler(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/article/deleteById"
        get(url,{id:row.id}).then( resp => {
          // 提示删除成功
          this.$message({type:"success",message:resp.message})
          // 刷新页面数据
          this.loadData();
        });
      })
    },
    //2.1 加载数据
    loadData(){
      let url = "/article/pageQuery";
      get(url,this.param).then( resp => {
        this.articles = resp.data;
      });
    }
  },
  // 3. 生命周期，在页面加载出来之前
  created(){
    this.loadData();
  }
}
</script>
